﻿using GN.Data.Entity;
using GN.Data.Entity.Attribute;
using System;
using System.Collections.Generic;
using System.Text;

namespace GN.Data
{
    public class DbContext
    {

        /// <summary>
        /// 新增到数据库
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddToDB<T>(T model) where T : EntityModel
        {
            System.Reflection.PropertyInfo[] info = typeof(T).GetProperties();
            StringBuilder fields = new StringBuilder();
            StringBuilder values = new StringBuilder();
            List<SqlClient.DataParameter> parameters = new List<SqlClient.DataParameter>();
            foreach (var property in info)
            {
                Identiy[] identitys = (Identiy[])property.GetCustomAttributes(typeof(Identiy), false);
                NoField[] nofield = (NoField[])property.GetCustomAttributes(typeof(NoField), false);
                if (identitys.Length == 0 && nofield.Length == 0)
                {
                    object value = property.GetValue(model, null);
                    fields.Append(property.Name + ",");
                    values.Append("@" + property.Name + ",");
                    parameters.Add(new SqlClient.DataParameter() { ParameterName = "@" + property.Name, SqlValue = value });
                }
            }
            fields.Remove(fields.Length - 1, 1);
            values.Remove(values.Length - 1, 1);

            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("insert into {0}(", typeof(T).Name);
            strSql.Append(fields.ToString() + ")");
            strSql.Append(" values (");
            strSql.Append(values.ToString() + ")");
            strSql.Append(";select @@IDENTITY");
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters.ToArray());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool UpdateToDB<T>(T model) where T : EntityModel
        {
            System.Reflection.PropertyInfo[] info = typeof(T).GetProperties();
            StringBuilder fields = new StringBuilder();
            StringBuilder whereid = new StringBuilder();
            List<SqlClient.DataParameter> parameters = new List<SqlClient.DataParameter>();
            foreach (var property in info)
            {
                Identiy[] identitys = (Identiy[])property.GetCustomAttributes(typeof(Identiy), false);
                NoField[] nofield = (NoField[])property.GetCustomAttributes(typeof(NoField), false);
                if (identitys.Length == 0 && nofield.Length == 0)
                {
                    #region 获取字段类型
                    //Type tt = item.PropertyType;
                    //string typeName = string.Empty;
                    //if (tt.IsGenericType == false)
                    //{
                    //    typeName = tt.Name.ToLower();
                    //}
                    //else
                    //{
                    //    Type[] ttt = tt.GetGenericArguments();
                    //    if (ttt.Length == 1)
                    //    {
                    //        typeName = ttt[0].Name.ToLower();
                    //    }
                    //}
                    #endregion
                    object val = property.GetValue(model, null);
                    fields.Append(property.Name + "=@" + property.Name + ",");
                    parameters.Add(new SqlClient.DataParameter() { ParameterName = "@" + property.Name, SqlValue = val });
                }
                else if (identitys.Length > 0)
                {
                    object val = property.GetValue(model, null);
                    whereid.Append(property.Name + "=@" + property.Name);
                    parameters.Add(new SqlClient.DataParameter() { ParameterName = "@" + property.Name, SqlValue = val });
                }
            }
            fields.Remove(fields.Length - 1, 1);

            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("update {0} set {1}", typeof(T).Name, fields.ToString());
            strSql.Append(" where " + whereid.ToString());
            int ret = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters.ToArray());
            if (ret > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
